package entity;

import java.text.*;
import java.util.*;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlSeeAlso;

import exception.IllegalInput;

import settings.Settings;



/**
 * @author A0098808L Yuen Shu Hui
 */
@XmlSeeAlso( {Income.class, Expense.class})
public abstract class CashFlow extends XMLentity{	
	public enum Period{
		YEARLY, MONTHLY, WEEKLY, DAILY
	}

	private String id;
	private String name;
	private Category category;
	private double amount;
	private Source source;
	private final Calendar date = new GregorianCalendar(); // initialized local private date variable
	private boolean isRecurs;
	private Period recursPeriod;
	private String recursParentID;

	/**
	 * @author A0098808L Yuen Shu Hui
	 * @return the id
	 */
	public String getId() {
		return id;
	}

	/**
	 * @author A0098808L Yuen Shu Hui
	 */
	@XmlAttribute
	public void setId(String _id) {
		this.id = _id;
	}

	/**
	 * @author A0098808L Yuen Shu Hui
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @author A0098808L Yuen Shu Hui
	 * @param set _name to this.name
	 */
	public void setName(String _name) {
		this.name = _name;
	}

	/**
	 * @author A0098808L Yuen Shu Hui
	 * @return the category
	 */
	public Category getCategory() {
		return category;
	}

	/**
	 * @author A0098808L Yuen Shu Hui
	 * @param set _category to this.category
	 */
	public void setCategory(Category _category) {
		this.category = _category;
	}

	/**
	 * @author A0098808L Yuen Shu Hui
	 * @return the amount
	 */
	public double getAmount() {
		return amount;
	}

	/**
	 * @author A0098808L Yuen Shu Hui
	 * @param set _amount to this.amount
	 */
	public void setAmount(double _amount) {
		this.amount = _amount;
	}

	/**
	 * @author A0098808L Yuen Shu Hui
	 * @return the source
	 */
	public Source getSource() {
		return source;
	}

	/**
	 * @author A0098808L Yuen Shu Hui
	 * @param set _source to this.source
	 */
	public void setSource(Source _source) {
		this.source = _source;
	}

	/**
	 * @author A0098808L Yuen Shu Hui
	 * @return the date
	 */
	public Calendar getDate() {
		return date;
	}

	/**
	 * @author A0098808L Yuen Shu Hui
	 * @param set _date to this.date
	 */
	public void setDate(Calendar _date) {
		if(_date != null)
			this.date.set(_date.get(Calendar.YEAR), _date.get(Calendar.MONTH), _date.get(Calendar.DATE));
	}

	/**
	 * @author A0098808L Yuen Shu Hui
	 * @return the isRecurs
	 */
	public boolean getRecurs() {
		return isRecurs;
	}

	/**
	 * @author A0098808L Yuen Shu Hui
	 * @param set _isRecurs to this.isRecurs
	 */
	public void setRecurs(boolean _isRecurs) {
		this.isRecurs = _isRecurs;
	}

	/**
	 * @author A0098808L Yuen Shu Hui
	 * Verified period before set it to recurs field
	 * @param _isRecurs
	 * @param _recursPeriod
	 * @throws IllegalInput
	 */
	public void SetAndVerifyRecurs(boolean _isRecurs, Period _recursPeriod) throws IllegalInput
	{
		if(_isRecurs == true){
			if(_recursPeriod == null)
			{
				throw new IllegalInput("Period cannot be NULL for recursive entry");
			}
			else
			{
				this.setRecurs(_isRecurs);
				this.setRecursPeriod(_recursPeriod);
			}
		}
		else
		{
			this.setRecurs(_isRecurs);
			this.setRecursPeriod(null);
			this.setRecursParentID(null);
		}
	}
	/**
	 * @author A0098808L Yuen Shu Hui
	 * @return the recursPeriod
	 */
	public Period getRecursPeriod() {
		return recursPeriod;
	}

	/**
	 * @author A0098808L Yuen Shu Hui
	 * @param set _recursPeriod to this.recursPeriod
	 */
	public void setRecursPeriod(Period _recursPeriod) {
		this.recursPeriod = _recursPeriod;
	}

	/**
	 * @author A0098808L Yuen Shu Hui
	 */
	public String getRecursParentID() {
		return recursParentID;
	}

	/**
	 * @author A0098808L Yuen Shu Hui
	 * @param _recursParentId
	 */
	@XmlAttribute
	public void setRecursParentID(String _recursParentId) {
		this.recursParentID = _recursParentId;
	}

	/**
	 * @author A0098808L Yuen Shu Hui
	 * for testing purpose -> to check whether did the variable are being set properly
	 */
	public String toString()
	{
		return (id + ", " + name + ", " + category.getName() +  ", " + category.getId()+ ", " + amount + ", " + source.getName() + ", " + Settings.getInstance().df.format(date.getTime()) + ", " + isRecurs + ", " + recursPeriod);
	}
}
